home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Applications / PICSee Dust 1.01 / Quaternary Source / Class_DynamoArray.cpp < prev    next >
Encoding:
Text File  |  1995-11-22  |  10.2 KB  |  142 lines  |  [TEXT/CWIE]

  1. ------------------------------------------------------
  2.  
  3. #pragma mark === Member Funcs ===
  4.  
  5. template <class DynamoArrayType>
  6. void DynamoArray<DynamoArrayType>::Preallocate(short size) {
  7.         /* This makes the array the specified size; note that
  8.            the old array is destroyed - it's contents are lost.
  9.            So make sure you call this first before adding any
  10.            data to the array.
  11.         */
  12.  
  13.     delete []fArray;
  14.  
  15.         // Make it a little bigger, just in case
  16.     fMaxSize = size + STD_DYNAMO_LEN;
  17.  
  18.     fArray = new DynamoArrayType[fMaxSize];
  19.     if (fArray == NULL) {
  20.         // Oops. Insert c++ exception here.
  21.     }
  22.  
  23.     fCurSize = 0;
  24. } // END Preallocate
  25.  
  26. //-------------------------------------------------------------
  27.  
  28. template <class DynamoArrayType>
  29. short DynamoArray<DynamoArrayType>::Get(DynamoArrayType& theItem, short index) {
  30.     if ((index < 0) || (index >= fCurSize) || (fArray == NULL))
  31.         // Check for correct boundaries...
  32.         return(kDynamoArrayOutOfBoundsErr);
  33.     else {
  34.         theItem = fArray[index];
  35.         return(index);
  36.     }
  37. } // END GetItem
  38.  
  39. //-------------------------------------------------------------
  40.  
  41. template <class DynamoArrayType>
  42. short DynamoArray<DynamoArrayType>::RandomGet(DynamoArrayType& theItem) {
  43.     if (fCurSize > 0) {
  44.         // Get a random value.
  45.         short randomVal = GetRandom(0, fCurSize - 1);
  46.         // Now retrieve the item.
  47.         short returnVal = Get(theItem, randomVal);
  48.         
  49.         // Did something go wrong? If no...
  50.         if (returnVal >= 0)
  51.             return(randomVal);
  52.         else
  53.             // Ooops, something went wrong; return error value.
  54.             return(returnVal);
  55.         }
  56.     else
  57.         // Oops, array empty.
  58.         return(kDynamoArrayOutOfBoundsErr);
  59. } // END RandomGet
  60.  
  61. //-------------------------------------------------------------
  62.  
  63. template <class DynamoArrayType>
  64. short DynamoArray<DynamoArrayType>::Append(const DynamoArrayType& theItem) {
  65.     if (fArray == NULL) {
  66.         // If array empty, create new one.
  67.         fMaxSize = STD_DYNAMO_LEN;
  68.         fCurSize = 1;
  69.         fArray = new DynamoArrayType[fMaxSize];
  70.     }
  71.     else {
  72.         if (fCurSize == fMaxSize)
  73.             // Ooh, we reached the maximum size. Time to enlarge
  74.             // the array.
  75.             IncreaseSize();
  76.         // We're going to add an item, so increment fCurSize
  77.         fCurSize++;
  78.     }
  79.  
  80.     // Put item into the array.
  81.     fArray[fCurSize - 1] = theItem;
  82.     // Return index value of item we appended into array.
  83.     return(fCurSize - 1);
  84. } // END Append
  85.  
  86. //-------------------------------------------------------------
  87.  
  88. template <class DynamoArrayType>
  89. short DynamoArray<DynamoArrayType>::Insert(const DynamoArrayType& theItem, short index) {
  90.     if ((index < 0) || (index > fCurSize) || (fArray == NULL))
  91.         return(kDynamoArrayOutOfBoundsErr);
  92.     
  93.     if (fCurSize + 1 == fMaxSize)
  94.         // Reached maximum size, so enlarge the array.
  95.         IncreaseSize();
  96.  
  97.     // OK, make room for our new item! Shift everything down from
  98.     // location on to the end of the array.
  99.     for (short i = fCurSize - 1; i >= index; i--)
  100.         fArray[i + 1] = fArray[i];
  101.     
  102.     // Add our new item.
  103.     fArray[index] = theItem;
  104.  
  105.     // We're adding an item, so increment the number of items in array.
  106.     fCurSize++;
  107.     
  108.     return(index);    // Everything OK, so return index value.
  109. } // END Insert
  110.  
  111. //-------------------------------------------------------------
  112.  
  113. template <class DynamoArrayType>
  114. short DynamoArray<DynamoArrayType>::Delete(short index) {
  115.     if ((index < 0) || (index >= fCurSize) || (fArray == NULL))
  116.         return(kDynamoArrayOutOfBoundsErr);
  117.     
  118.     // Shift array backwards (down to 0), so we don't lose anything.
  119.     // If user specified last item is the item to be deleted, this loop
  120.     // won't execute. We just decrement fCurSize instead.
  121.     for (short i = index + 1; i < fCurSize; i++) 
  122.         fArray[i - 1] = fArray[i];
  123.  
  124.     fCurSize--;
  125.     return(index);
  126. } // END Delete
  127.  
  128. //-------------------------------------------------------------
  129.  
  130. template <class DynamoArrayType>
  131. short DynamoArray<DynamoArrayType>::SearchDelete(DynamoArrayType& theItem, short startIndex) {
  132.     short val = kDynamoArrayOutOfBoundsErr;    // Assume did not find item
  133.  
  134.     // Search for the item by value, not by index.
  135.     val = Search(theItem, startIndex);
  136.     if (val >= 0)
  137.         Delete(val);    // We found it, so delete!
  138.  
  139.     return val;
  140. } // END SearchDelete
  141.  
  142. //----------